package com.training.java.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class EmpDaoJdbcImpl implements EmployeeDAO { 
	
	private String url;
	private String username;
	private String password;
	
	Connection connection = null;
	Statement statement = null;
	ResultSet resultSet = null;
	List<Employee> employeeList;
	
	public EmpDaoJdbcImpl (String url,String username, String password)
	{
		this.url=url;
		this.username=username;
		this.password=password;
	}

	@Override
	public List<Employee> getAllEmployees() throws DAOException {
		
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			connection=DriverManager.getConnection(url, username, password);
			statement=connection.createStatement();
			resultSet= statement.executeQuery("select * from employees");
			
				while(resultSet.next())
				{
					int employeeId= resultSet.getInt("employee_id");
					String firstName= resultSet.getString("first_name");
					String lastName=resultSet.getString("last_name");
					String email=resultSet.getString("email");
					String phoneNumber=resultSet.getString("phone_number");
					Date hireDate=resultSet.getDate("hire_date");
					String jobId=resultSet.getString("job_id");
					
					System.out.println("Employee Id: "+ employeeId);
					System.out.println("first name: "+ firstName);
					System.out.println("Last Name: "+ lastName);
					System.out.println("Email: "+ email);
					System.out.println("Phone Number: "+ phoneNumber);
					System.out.println("Hire Date: "+ hireDate);
					System.out.println("JOb Id: "+ jobId);
					System.out.println("***************************");
					
					Employee employee= new Employee(employeeId,firstName,lastName,email,phoneNumber,hireDate,jobId);
					employeeList= new ArrayList<Employee>();
					employeeList.add(employee);
					
									
				}
			
					
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			
		}catch(SQLException ex)
		{
			throw new DAOException(ex);
			
		}
		finally
		{
			if (resultSet!=null)
			{
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(statement!=null)
			{
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(connection!=null)
			{
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}	
		return employeeList;
	}

	@Override
	public void addEmployee(Employee employee) {
		
	}

	@Override
	public void updateEmployeeInfo(Employee employee) {
		
	}

	@Override
	public void deleteEmployee(int employeeId) {
		
	}

	@Override
	public Employee findEmployeeByPrimaryKey(int empid) {
		return null;
	}
	

}
